<template>
  <el-card>
    <div>
      <!-- 1.0 查询表单 -->
      <el-form
        :inline="true"
        :model="searchObj"
        class="demo-form-inline"
        size="mini"
      >
        <el-form-item label="订单号">
          <el-input v-model="searchObj.orderNo" placeholder="订单号"></el-input>
        </el-form-item>
        <el-form-item label="收货人">
          <el-input
            v-model="searchObj.consignee"
            placeholder="收货人"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="searchObj.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="searchObj.orderState" placeholder="订单状态">
            <el-option label="已完成" value="已完成"></el-option>
            <el-option label="已受理" value="已受理"></el-option>
            <el-option label="派送中" value="派送中"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择时间">
          <el-date-picker
            v-model="searchObj.date"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getOrders('s')">查询</el-button>
        </el-form-item>
      </el-form>

      <!-- 2.0 表格 -->
      <el-table :data="tableData" border style="width: 100%" size="mini">
        <el-table-column prop="orderNo" label="订单号"> </el-table-column>
        <el-table-column label="下单时间" width="150px">
          <template v-slot="{ row }">
            {{ row.orderTime | format }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号"> </el-table-column>
        <el-table-column prop="consignee" label="收货人"> </el-table-column>
        <el-table-column prop="deliverAddress" label="配送地址">
        </el-table-column>
        <el-table-column label="送达时间" width="150px">
          <template v-slot="{ row }">
            {{ row.deliveryTime | format }}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="用户备注"> </el-table-column>
        <el-table-column prop="orderAmount" label="订单金额"> </el-table-column>
        <el-table-column prop="orderState" label="订单状态"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template v-slot="{ row }">
            <el-button type="text" size="small" @click="fn(row)"
              >查看</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="$router.push(`/order/edit/${row.id}`)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 查看弹框 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="40%">
        <div>
          <!-- 步骤条 -->
          <el-steps direction="vertical" :active="curNum">
            <el-step title="已受理"></el-step>
            <el-step title="派送中"></el-step>
            <el-step title="已完成" description="已送达,赶紧评价"></el-step>
          </el-steps>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <!-- 3.0 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import { allOrders, searchOrder } from '../../api/orders.js'
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      // 数据对象
      searchObj: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: ''
      },
      dialogVisible: false,
      curNum: 1
    }
  },
  created() {
    this.getOrders()
  },
  methods: {
    async getOrders(s) {
      const obj = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        ...this.searchObj
      }
      let result = null
      if (s) {
        obj.currentPage = 1
        obj.date =
          JSON.stringify(obj.date) === 'null' ? '' : JSON.stringify(obj.date)
        // console.log(obj)
        result = await searchOrder(obj)
      } else {
        result = await allOrders({
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          orderNo: '',
          consignee: '',
          phone: '',
          orderState: '',
          date: ''
        })
      }
      // console.log(data)
      const { data, total } = result
      this.tableData = data
      this.total = total
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getOrders()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getOrders()
    },
    fn(row) {
      this.dialogVisible = true
      const obj = {
        已受理: 1,
        派送中: 2,
        已完成: 3
      }
      this.curNum = obj[row.orderState]
    }
  }
}
</script>

<style lang="less" scoped>
.el-input,
.el-select {
  width: 150px;
}
.el-date-editor {
  width: 300px;
}
</style>
